package com.shuoyue.ycgk.ui.mine.learnprogress.contract;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.DealLessonInfo;
import com.shuoyue.ycgk.entity.MouthInfo;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;
import com.shuoyue.ycgk.utils.XDateUtils;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import io.reactivex.Observable;

public interface DealClassProgressContract {
    interface View extends BaseView {
        void setLessonProcess(DealLessonInfo lessonProcess);

        void setMouthInfo(Date date, List<MouthInfo> infos);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getProgressInfo(String tagetDate) {
            apply(model.getProgress(1851, null, tagetDate))
                    .subscribe(new ApiSubscriber<Optional<DealLessonInfo>>(mView, this) {
                        @Override
                        public void onNext(Optional<DealLessonInfo> lessonProcessOptional) {
                            super.onNext(lessonProcessOptional);
                            mView.setLessonProcess(lessonProcessOptional.getIncludeNull());
                        }

                        @Override
                        public void onFinish() {
                            mView.setLessonProcess(null);
                        }
                    });
        }

        public void getMouthInfos(Date date) {
            String dateStr = XDateUtils.format(date.getTime(), "yyyy-MM");
            apply(model.getMouthInfos(dateStr))
                    .subscribe(new ApiSubscriber<Optional<List<MouthInfo>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<MouthInfo>> lessonProcessOptional) {
                            super.onNext(lessonProcessOptional);
                            mView.setMouthInfo(date, lessonProcessOptional.getIncludeNull());
                        }

                        @Override
                        public void onFinish() {
                            mView.setLessonProcess(null);
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<DealLessonInfo>> getProgress(int memberId, Integer subjectId, String date) {
            return RetrofitClient.getInstance().getApi().getDealLearnProcess(memberId, subjectId, date);
        }

        Observable<BaseResult<List<MouthInfo>>> getMouthInfos(String date) {
            return RetrofitClient.getInstance().getApi().getMouthInfos(date);
        }
    }
}
